import { observable, action, toJS } from 'mobx';
import * as Detail from '../apis/detail';
import { message, Modal } from 'antd';

const { confirm } = Modal;
import * as DocLog from '../apis/docLog';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;

class DocLogStore {
  @observable title= '文档日志';
  @observable loading= false;
  @observable showSearchAd= false;
  @observable fields= {};
  @observable dataKey= '';
  @observable conditionInfo= [];
  @observable topTab= [];
  @observable topTabKey= '';
  @observable rightMenu={};
  @observable tableStore = new TableStore();
  @observable form = new WeaForm();

  @action
  setShowSearchAd = (showSearchAd) => {
    this.showSearchAd = showSearchAd;
  }
  @action
  saveFields = (fields = {}) => {
    this.fields = fields;
    this.topTabKey = (function () {
      let topTabKey = '';// topTabKey
      if (fields) {
        for (let key in fields) {
          if (fields[key].name == 'viewcondition') {
            topTabKey = fields[key].value;
          }
        }
      }
      return topTabKey;
    }());
  }
  @action
  getTopTab = (params) => {
    DocLog.getTopTab(params).then(action((datas) => {
      this.topTab = datas.groupinfo;
    }));
  }
  @action
  getConditionInfo = (params) => {
    DocLog.getCondition(params).then(action((data) => {
      this.conditionInfo = data.condition;
      this.form.initFormFields(data.condition);
    })).catch(err => message.error(err));
  }
  @action
  doSearch = (params) => {
    this.loading = true;
    let viewcondition = this.getSelectedKey();
    let _params = { ...params, ...this.form.getFormParams(), viewcondition };
    DocLog.getTable(_params).then(action((data) => {
      this.tableStore.getDatas(data.sessionkey, params.current || 1);
      this.loading = false;
    })).catch(err => message.error(err));
  }
  @action
  getRightMenu = (params) => {
    DocLog.getRightMenu(params).then(action((data) => {
      this.rightMenus = data;
    })).catch(err => message.error(err));
  }

  @action
  getSelectedKey = () => {
    const { topTab, topTabKey } = this;
    let selectedTab = '';
    topTab && toJS(topTab).forEach((t) => {
      if (t.selected) {
        selectedTab = t.viewcondition;
      }
    });
    topTabKey && (selectedTab = topTabKey);
    return selectedTab;
  }
}

export default DocLogStore;
